﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using CoreInmobiliario.Data;

namespace CoreInmobiliario.Business
{
    [DataObject]
    public class Caracteristica
    {
        public static Caracteristica GetInstance()
        {
            return new Caracteristica();
        }

        private static DBDataContext GetDBDataContext()
        {
            Factories.DBDataContextFactory factory = new Factories.DBDataContextFactory();
            return factory.CreateDataContext() as DBDataContext;
        }

        public Data.Caracteristica GetById(int caracteristicaId)
        {
            Data.Caracteristica caracteristica = null;

            using (DBDataContext dataContext = GetDBDataContext())
            {
                try
                {
                    caracteristica = dataContext.Caracteristicas.FirstOrDefault(c => c.CaracteristicaId.Equals(caracteristicaId));
                }
                catch (Exception ex)
                {

                }
            }
            return caracteristica;
        }

        public List<Data.Caracteristica> GetList()
        {
            List<Data.Caracteristica> list = null;

            using (DBDataContext dataContext = GetDBDataContext())
            {
                try
                {
                    list = dataContext.Caracteristicas.ToList();

                }
                catch (Exception ex)
                {

                }
            }
            return list;
        }

        public List<Data.Caracteristica> GetListCaracteristicaByPropiedadId(int propiedadId)
        {
            List<Data.Caracteristica> list = null;

            using (DBDataContext dc = GetDBDataContext())
            {
                try
                {
                    var query = from p in dc.GetTable<Data.Propiedad>()
                                from pc in dc.GetTable<Data.PropiedadCaracteristica>()
                                from c in dc.GetTable<Data.Caracteristica>()
                                where
                                    (pc.PropiedadId == p.PropiedadId) &&
                                    (p.PropiedadId == propiedadId) &&
                                    (c.CaracteristicaId == pc.CaracteristicaId)
                                select c;

                    list = query.ToList();
                }
                catch (Exception ex)
                {
                    
                }
            }

            return list;
        }
    }
}
